/*
 * Copyright (c) 2013 Andaily Information Technology Co. Ltd
 * www.andaily.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Andaily Information Technology Co. Ltd ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with Andaily Information Technology Co. Ltd.
 */
package top.suven.http.oauth.token;

import top.suven.http.oauth.validator.AbstractOauthGrantTypeValidator;
import top.suven.http.oauth.validator.GrantTypeAuthorizationCodeValidator;
import org.apache.oltu.oauth2.as.request.AbstractOAuthTokenRequest;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.apache.oltu.oauth2.common.message.types.GrantType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @ClassName: AuthorizationCodeTokenHandler.java
 * @Description: OAUTH2.0的认证服务, auth 请求验证 grant_type=authorization_code
 * @Author suven.wang
 * @Date 2018/7/26 18:01
 * @Copyright: (c) 2018 gc by https://www.gc.com.cn/
 * @Version : 1.0.0
 *  --------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 *  --------------------------------------------------------
 */
public class AuthorizationCodeTokenHandler extends AbstractOAuthTokenHandler {

    private static final Logger LOG = LoggerFactory.getLogger(AuthorizationCodeTokenHandler.class);

    @Override
    public boolean support(AbstractOAuthTokenRequest oauthTokenRequest) {
        super.support(oauthTokenRequest);
        final String grantType = oauthTokenRequest.getGrantType();
        return GrantType.AUTHORIZATION_CODE.toString().equalsIgnoreCase(grantType);
    }

    /*
    *
    * /oauth/token?client_id=unity-client&client_secret=unity&grant_type=authorization_code&code=zLl170&redirect_uri=redirect_uri
    * */
    @Override
    public OAuthResponse handleAfterValidation() {

//        OAuthResponse tokenResponse =  this.createTokenResponse(false);
        try {
//            OAuthTokenBuilder tokenBuilder = typeValidator.saveToken();
//            typeValidator.successToken(tokenBuilder,)

        }catch (Exception e){

        }

        //remove code lastly
        removeCode();
        return null;
    }

    private void removeCode() {
        final String code = tokenRequest.getCode();
        final boolean result = true;
        // todo
        // oauthService.removeOauthCode(code, clientDetails());
        LOG.debug("Remove code: {} result: {}", code, result);
    }



    protected AbstractOauthGrantTypeValidator getValidator() {

        return new GrantTypeAuthorizationCodeValidator(tokenRequest);
    }

}
